package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Override
    public void saveSetmeal(SetmealDto setmealDto) {
        /*
        此时数据封装到了SetmealDto中，对基本数据添加到套餐表中，对菜品设置一个套餐id，并添加到第三方表中
         */
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> collect = setmealDishes.stream().map((item) -> {//item为SetmealDish对象
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(collect);
    }


    @Override
    public void delete(List<Long> ids) {
        /*
         0.查出起售状态的数据，并抛出异常，不是起售状态才能删除
        1.先删除套餐表中的数据
        2.根据套餐表中的id去setmealdish表中找出相应的菜品进行删除
        3.删除先关联菜品数据
         */
        //select * from setmeal where status=1 and id in(1,2,3)
        if (ids.size()==0){
            throw new CustomException("请选择要删除的套餐");
        }
        LambdaQueryWrapper<Setmeal> sqlw = new LambdaQueryWrapper<>();
        sqlw.eq(Setmeal::getStatus, 1);
        sqlw.in(Setmeal::getId, ids);
        int count = this.count(sqlw);
        if (count>0){//说明当前这个为起售状态的，不能删除，抛出异常
            throw new CustomException("当前商品为售卖状态，不能删除");
        }
        this.removeByIds(ids);
//        setmealDishService.removeByIds(ids);这个方法并不能删除菜品，因为他是根据setmealDish中的id删除
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(lqw);
    }

    @Override
    public SetmealDto dateHX(Long id) {
        /*
        id为套餐id,查询到套餐菜品表,封装到dto中
         */

        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(lqw);
        SetmealDto setmealDto = new SetmealDto();
        setmealDto.setSetmealDishes(list);
        //根据套餐id找到套餐名字
        Setmeal setmeal = this.getById(id);
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setCategoryName(setmeal.getName());
        return setmealDto;

    }

    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
       /*
       分析：次数据来源与2张表，取出每张表数据，如何对于套餐菜品表先删除后添加。套餐菜品表根据套餐id进行查询对于菜品
        */

       this.updateById(setmealDto);//根据套餐id更新套餐

        LambdaQueryWrapper<SetmealDish> setmealDishlqw = new LambdaQueryWrapper<>();
        setmealDishlqw.eq(SetmealDish::getSetmealId,setmealDto.getId());

        setmealDishService.remove(setmealDishlqw);//删除数据库中原有的套餐数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> collect = setmealDishes.stream().map((item) -> {//套餐和菜品一对多
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(collect);

    }
}
